package com.example.fengyungo.utils;

import android.util.Log;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.alibaba.fastjson.JSONObject;
import com.example.fengyungo.R;
import com.example.fengyungo.constant.MessageConstant;
import com.example.fengyungo.constant.ResultConstant;
import com.example.fengyungo.domain.Message;

import java.io.IOException;

/**
 * @author 徐
 */
public class InfoUtil {
    private static final String TAG = "InfoUtil";

    /**
     * 在聊天列表添加通知
     *
     * @param fragmentActivity FragmentActivity
     * @param userId           String，用户id
     */
    public static void addMessageList(FragmentActivity fragmentActivity, String userId) {
        Runnable runnable = () -> {
            try {
                String url = MessageConstant.URL + "/addMessageList/" + MessageConstant.D_SYSTEM_ID + "/" + userId;
                String responseData = OkHttpUtils.post(url, "");
                Log.d(TAG, "addMessageList: responseData=" + responseData);
                JSONObject jsonObject = JSONObject.parseObject(responseData);
                int code = jsonObject.getInteger(ResultConstant.CODE);
                String msg = jsonObject.getString(ResultConstant.MSG);
                if (code == ResultConstant.SUCCESS) {
                    Log.d(TAG, "addMessageList: success");
                } else {
                    fragmentActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(fragmentActivity, msg, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
                fragmentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(fragmentActivity, fragmentActivity.getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        ThreadPoolUtil.execute(runnable);
    }

    /**
     * 发送通知
     *
     * @param fragmentActivity FragmentActivity
     * @param message          内容
     */
    public static void sendMessage(FragmentActivity fragmentActivity, Message message) {
        Runnable runnable = () -> {
            try {
                String url = MessageConstant.URL + "/addMessage";

                String json = JSONObject.toJSONString(message);

                String responseData = OkHttpUtils.post(url, json);
                Log.d(TAG, "addMessage: responseData=" + responseData);
                JSONObject jsonObject = JSONObject.parseObject(responseData);
                int code = jsonObject.getInteger(ResultConstant.CODE);
                String msg = jsonObject.getString(ResultConstant.MSG);
                if (code == ResultConstant.SUCCESS) {
                    Log.d(TAG, "addMessage: success");
                } else {
                    fragmentActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(fragmentActivity, msg, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
                fragmentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(fragmentActivity, fragmentActivity.getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
    }
}
